﻿using LY.Common;
using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace LY.Business.ServiceImpl
{
    public class FundsFlowService : IFundsFlowService
    {
        FundsFlowRepository _repo;
        InvestRepository _investment;
        public FundsFlowService()
        {
            _repo = new FundsFlowRepository();
            _investment = new InvestRepository();
        }
        //资金流列表
        public PagedList<FundsFlow_EX> QueryFundsFlowList(FundsFlowSearch search, PageView view)
        {
             search.Condition = Utility.ClearSafeStringParma(search.Condition);
             search.Sdate = Utility.ClearSafeStringParma(search.Sdate);
             search.Edate = Utility.ClearSafeStringParma(search.Edate);
             return _repo.QueryFundsFlowList(search,view);
        }
        public List<FundsFlow_EX> QueryFundsFlowList(int pageindex, int memberId)
        {
            return _repo.QueryFundsFlowList(pageindex, memberId);
        }
        public int QueryFundsFlowCount(int memberId)
        {
            return _repo.QueryFundsFlowCount(memberId);
        }
        //待提现  导出
        public List<FundsFlow_EX> QueryFundsExportList(FundsFlowSearch search)
        {
            return _repo.QueryFundsExportList(search);
        }
        //获取资金实体
        public FundsFlow_EX GetFundsFlow(int? id)
        {
            return _repo.GetFundsFlow(id);
        }

        //审核保存 
        public int AuditSave(int? id, int status, string remark)
        {
            remark = Utility.ClearSafeStringParma(remark);
            return _repo.AuditSave(id,status,remark);
        }

        //充值
        public int FundsFlowSave(U_FundsFlow fundsflow,string fee)
        {
            int ret = -1;
            if (fundsflow.Type == 1)//充值
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    _repo.Insert(fundsflow);
                    DateTime now = DateTime.Now;
                    Log_FundsFlow log_f = new Log_FundsFlow //记录日志
                    {
                        OrderNo = fundsflow.OrderNo,
                        MemberId = fundsflow.MemberId,
                        RechargeAmt = fundsflow.OutPay.ToString(),
                        RechargeDate = now.ToString("yyyyMMddhhmmss"),
                        CreateDate = now,
                        Status = 1,
                        BankName = "",
                        Type = 2 //提现
                    };
                    _repo.Insert(log_f);
                    scope.Complete();
                    ret = 1;
                }
            }
            else if (fundsflow.Type == 2)//提现
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    DateTime now = DateTime.Now;
                    //fundsflow.OrderNo = "HJH" + now.ToString("yyMMddhhmmss") + new Random().Next(10000, 99999).ToString();
                    //这里提现的订单号应该是盛付通那边返回的订单号
                    //资金表
                    _repo.Insert(fundsflow);
                    if (!string.IsNullOrEmpty(fee))//不为空就要手续费了。
                    {
                        fundsflow.OutPay=decimal.Parse(fee);
                        fundsflow.Type = 6;//提现手续费
                        
                        //fundsflow.OrderNo = "HJH" + now.ToString("yyMMddhhmmss") + new Random().Next(10000, 99999).ToString();
                        _repo.Insert(fundsflow);
                    }
                    Log_FundsFlow log_f = new Log_FundsFlow //记录日志
                    {
                        OrderNo = fundsflow.OrderNo,
                        MemberId = fundsflow.MemberId,
                        RechargeAmt = fundsflow.OutPay.ToString(),
                        RechargeDate = now.ToString("yyyyMMddhhmmss"),
                        CreateDate = now,
                        Status = 1,
                        BankName = "",
                        Type=2 //提现
                    };
                    _repo.Insert(log_f);
                    scope.Complete();
                    ret = 1;
                }
            }
            return ret;
        }

        //账户资金
        public FundsFlow_EX GetAccountInfo(int memberId)
        {
            return _repo.GetAccountInfo(memberId);
        }

        //验证订单唯一性
        public int CheckOrderNo(string orderNo)
        {
            return _repo.CheckOrderNo(orderNo);
        }

        //每个会员一天最多能提现3次
        public int CheckDrawalCount(int memberId)
        {
            return _repo.CheckDrawalCount(memberId);
        }

        //投标
        public int BiaoFundsFlow(U_FundsFlow fundsflow)
        {
            if (fundsflow.Id > 0) //编辑
            {
                return _repo.Update(fundsflow);
            }
            else
            {
                return (int)_repo.Insert(fundsflow);
            }
        }

        //根据投标的投标号来获取当前的资金记录，投标号是不能重复的。
        public U_FundsFlow GetFundsFlowByOrderNo(string orderNo)
        {
            return _repo.GetFundsFlowByOrderNo(orderNo);
        }

        //根据投标的投标号和类型 来获取当前的资金记录
        public U_FundsFlow GetFundsFlowByOrderNo(string orderNo, int type)
        {
            return _repo.GetFundsFlowByOrderNo(orderNo, type);
        }

        public int UpdateFundsFlow(U_FundsFlow fundsflow)
        {
            return _repo.Update(fundsflow);
        }

        //查询当前用户充值的记录
        public PagedList<U_FundsFlow> QueryFundsFlowList2(FundsFlowSearch search, PageView view)
        {
            return _repo.QueryFundsFlowList2(search, view);
        }

        public int InsertFundsFlow(U_FundsFlow fundsflow)
        {
            return (int)_repo.Insert(fundsflow);
        }

        //查询当前用户的余额
        public decimal QueryBalance(int memberId)
        {
            return _repo.QueryBalance(memberId);
        }

        //流标处理
        public int LiuBiaoHandel(int parojectId)
        {
            List<U_FundsFlow_EX> list = _repo.QueryInvestmemtByProjectId(parojectId); //获取当前项目所有投标的人和金额，以人分组
            foreach (var oldfundsflow in list)
            {
                U_FundsFlow newFundsFlow = new U_FundsFlow();
                newFundsFlow.MemberId = oldfundsflow.MemberId;
                newFundsFlow.InCome = oldfundsflow.SumOutPay;
                newFundsFlow.CreateTime = DateTime.Now;
                newFundsFlow.Status = 1;
                newFundsFlow.Type = 12; //流标退款
                newFundsFlow.ProjectId = parojectId;
                _repo.Insert(newFundsFlow); //添加还款记录
            }
            //再修改所有的投资记录
            _investment.LiuBiaoTuiKuan(parojectId);
            return 1;
        }
    }
}
